package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.AttendanceQueryContent;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * Created by lenovo on 2016/11/18.
 * 考勤查询
 */
public class AttendanceQueryFragment extends BaseFragment implements View.OnClickListener {

  private static final int REQUEST_STARTTIME = 0;
  private static final int REQUEST_ENDTIME = 1;
  private static final int REQUEST_NAME = 2;
  private static final int REQUEST_SING_TYPE = 3;
  private AttendanceQueryContent mcontent;
  private TextView mtvName;// 员工
  private TextView mtvBegainTime;// 开始时间
  private TextView mtvEndTime;// 结束时间
  private TextView mtvType;// 打卡类型

  private String checkType;
  private String begincheckTime;
  private String endcheckTime;
  private String name;
  private String jobNos;

  public static AttendanceQueryFragment newInstance(AttendanceQueryContent content) {
    AttendanceQueryFragment mfragment = new AttendanceQueryFragment();
    Bundle bundle = new Bundle();
    bundle.putSerializable(AttendanceMapFragment.EXTRA_QUERY, content);
    mfragment.setArguments(bundle);
    return mfragment;
  }

  @Override public void initData() {
    super.initData();
    mcontent =
        (AttendanceQueryContent) getArguments().getSerializable(AttendanceMapFragment.EXTRA_QUERY);
    if (mcontent == null) return;
    name = mcontent.getName();
    jobNos = mcontent.getJobNos();
    checkType = mcontent.getCheckType();
    begincheckTime = mcontent.getBegincheckTime();
    endcheckTime = mcontent.getEndcheckTime();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_attendance_query, container, false);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(R.string.query);
    mtvBegainTime = (TextView) view.findViewById(R.id.tv_startTime);
    mtvEndTime = (TextView) view.findViewById(R.id.tv_endTime);
    mtvName = (TextView) view.findViewById(R.id.tv_staffName);
    mtvType = (TextView) view.findViewById(R.id.tv_signType);
    mtvName.setText(mcontent.getName());
    if (AttendanceMapFragment.IN.equalsIgnoreCase(mcontent.getCheckType())) {
      mtvType.setText(R.string.sign_in);
    } else if (AttendanceMapFragment.OUT.equalsIgnoreCase(mcontent.getCheckType())) {
      mtvType.setText(R.string.sign_out);
    } else if (AttendanceMapFragment.KEEP.equalsIgnoreCase(mcontent.getCheckType())) {
      mtvType.setText(R.string.sign_field_time);
    }
    mtvBegainTime.setText(mcontent.getBegincheckTime());
    mtvEndTime.setText(mcontent.getEndcheckTime());

    mtvName.setOnClickListener(this);
    mtvType.setOnClickListener(this);
    mtvBegainTime.setOnClickListener(this);
    mtvEndTime.setOnClickListener(this);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        //                if (NavUtils.getParentActivityName(getActivity())!=null){
        //                    NavUtils.navigateUpFromSameTask(getActivity());
        //                }
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        mcontent =
            new AttendanceQueryContent(name, jobNos, checkType, begincheckTime, endcheckTime);
        Intent i = new Intent();
        i.putExtra(AttendanceMapFragment.EXTRA_QUERY, mcontent);
        getActivity().setResult(Activity.RESULT_OK, i);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      // 开始时间
      case R.id.tv_startTime:
        showDialog(REQUEST_STARTTIME,
            MyDateUtil.stringToDate(begincheckTime, MyDateUtil.TYPE_DATE));
        break;
      // 结束时间
      case R.id.tv_endTime:
        showDialog(REQUEST_ENDTIME, MyDateUtil.stringToDate(endcheckTime, MyDateUtil.TYPE_DATE));
        break;
      // 获取员工列表
      case R.id.tv_staffName:
        //                Intent i = new Intent(getActivity(), SearchPersonMultipleActicity.class);
        Intent i = new Intent(getActivity(), SearchPersonActivity.class);
        startActivityForResult(i, REQUEST_NAME);
        break;

      case R.id.tv_signType:
        showDialog(REQUEST_SING_TYPE, null);
        break;

      default:
        break;
    }
  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   * @param date 对话框初始显示时间
   */
  public void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_STARTTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, 0);
        break;
      case REQUEST_ENDTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, 0);
        break;

      case REQUEST_SING_TYPE:
        //                dialog = new AttendanceTypeDialogFragment();
        dialog = new AttendanceQueryTypeDialogFragment();
        break;
      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    if (dialog == null) return;
    dialog.setTargetFragment(AttendanceQueryFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_STARTTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      //noinspection deprecation
      begincheckTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mtvBegainTime.setText(begincheckTime);
    }

    if (requestCode == REQUEST_ENDTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      //noinspection deprecation
      endcheckTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mtvEndTime.setText(endcheckTime);
      return;
    }

    if (requestCode == REQUEST_NAME && data != null) {
      CustomerDialog dialog =
          (CustomerDialog) data.getSerializableExtra(SearchPersonListFragment.EXTRA_VALUE);
      name = dialog.getName();
      jobNos = dialog.getJobNo();
      mtvName.setText(name);
    }

    if (requestCode == REQUEST_SING_TYPE && data != null) {
      checkType = data.getStringExtra(AttendanceQueryTypeDialogFragment.EXTRA_SIGN_TYPE);
      if (AttendanceMapFragment.IN.equalsIgnoreCase(checkType)) {
        mtvType.setText(R.string.sign_in);
      } else if (AttendanceMapFragment.OUT.equalsIgnoreCase(checkType)) {
        mtvType.setText(R.string.sign_out);
      } else if (AttendanceMapFragment.KEEP.equalsIgnoreCase(checkType)) {
        mtvType.setText(R.string.sign_field_time);
      }
    }

    super.onActivityResult(requestCode, resultCode, data);
  }
}
